//
//  NewFeatureCollectionViewController.swift
//  DSWeibo
//
//  Created by J on 16/4/17.
//  Copyright © 2016年 J. All rights reserved.
//  Swift中 private修饰的东西,如果是在一个文件中时可以访问的  只有不同文件才不能访问

import UIKit

class NewFeatureCollectionViewController: UICollectionViewController{

    /// 新特性页面个数
    private let pageCount = 4
    private var layout: NewFeatureLayout =  NewFeatureLayout()
    private let cellReuseIdentifier = "forCellWithReuseIdentifier"

    init(){//这里不用override 因为系统指定的构造方法是    init(collectionViewLayout layout: UICollectionViewLayout) {}
        super.init(collectionViewLayout: layout)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册一个cell
        collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
          }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //2.设置collectionView属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }
    
    
    
   }
extension NewFeatureCollectionViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        //1.获取cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as!NewFeatureCell
        //2.设置cell的数据
        cell.backgroundColor = UIColor.orangeColor()
        cell.imageIndex = indexPath.item

        //3.返回cell
        return cell
        
    }
    
    
    //完全显示cell之后调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //拿到当前现实cell的索引
        let path = collectionView.indexPathsForVisibleItems().last!
        let cell = collectionView.cellForItemAtIndexPath(path) as!NewFeatureCell
        if path.item == pageCount - 1 {
            cell .startAnimation()
        }
        
    }
    
    
}

//Swift一个文件中可以定义多个类
//如果当前类需要监听按钮点击方法 当前类不能是private
 class NewFeatureCell :UICollectionViewCell{
    
    ///保存图片索引
    private var imageIndex: Int?{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
            startButton.hidden = true

            }
    }
    
    
    
    
    
    func startAnimation(){
        startButton.hidden = false
        //执行动画
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue:0), animations: {
            
            self.startButton.transform = CGAffineTransformIdentity//清空形变
            self.startButton.userInteractionEnabled = true
            
        }) { (Bool) in

        }
        
    }
    
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //1.初始化UI
        setUpUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
   private func setUpUI(){
    
    //1.添加子控件到contentView
   contentView.addSubview(iconView)
    //2.布局子控件位置
    iconView.snp_makeConstraints { (make) in
        make.edges.equalTo(contentView)

    }
    
    contentView.addSubview(startButton)

    startButton.snp_makeConstraints(closure: { (make) in
        make.centerX.equalTo(self)
        make.bottom.equalTo(self).offset(-100)
    })
    
    
    
    }
    
    //MARK: -懒加载
    private lazy var iconView  = UIImageView()

    private lazy var startButton:UIButton = {//进入微博按钮
        let btn = UIButton()
        btn.hidden = true
        btn.setBackgroundImage(UIImage(named:"new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: #selector(NewFeatureCell.click), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    

    //进入微博
     func click(){
       //去主页  注意点: 企业开发中 如果要切换根控制器最好都在appdelegate中切换
        
        NSNotificationCenter.defaultCenter().postNotificationName(XMGSwitchRootViewControllerKey, object: true)
        
    }
  
    
    
}

/// 自定义布局对象
private class NewFeatureLayout: UICollectionViewFlowLayout {
    
    override func prepareLayout() {//cell准备布局  1.先调用过一共有多少行cell  2.准备布局  3.调用返回cell方法
        //1.设置layout布局
        itemSize = UIScreen.mainScreen().bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal

    }
    
}